/**
 * Created by 严秋旺 on 2016/11/1.
 */
(function (window) {
    var longEdit = function (obj, url, id) {
        var _this = this;
        var e = $('<span class="glyphicon glyphicon-edit leditico"></span>');
        $(obj).append(e);
        $(obj).each(function () {
            if ($(this).attr(longEdit.ATTR_URL) == undefined) {
                $(this).attr(longEdit.ATTR_URL, url);
            }
            if ($(this).attr(longEdit.ATTR_ID) == undefined) {
                $(this).attr(longEdit.ATTR_ID, id);
            }
        });
        $(obj).click(function (e) {
            var type = $(this).attr(longEdit.ATTR_TYPE);
            longEdit.ui[type].init(this);
        });
    };
    longEdit.ATTR_TYPE = 'etype';//编辑类型
    longEdit.ATTR_URL = 'eurl';//保存url
    longEdit.ATTR_ID = 'eid';//数据ID
    longEdit.ATTR_MSG = 'emsg';//
    longEdit.ATTR_DID = 'edid';//字典ID
    longEdit.KEY_ESCAPE = 27;//编辑取消按键值
    longEdit.KEY_ENTER = 13;//编辑保存按键值
    longEdit.cancel = function (control, btn) {
        var type = control != undefined ? $(control).attr(longEdit.ATTR_TYPE) : $(btn).attr(longEdit.ATTR_TYPE);
        longEdit.ui[type].cancel(control, btn);
    };
    longEdit.save = function (control, btn) {
        if (!confirm('确定保存修改吗？')) {
            return;
        }
        var type = control != undefined ? $(control).attr(longEdit.ATTR_TYPE) : $(btn).attr(longEdit.ATTR_TYPE);
        var root = control != undefined ? $(control).parent() : longEdit.baseUIRootElement(btn);
        var srcEle = $(root).prev();
        var name = srcEle.attr('name');
        var regx = '/^' + srcEle.attr('regx') + '$/';
        var id = srcEle.attr(longEdit.ATTR_ID);
        // var type = srcEle.attr(longEdit.ATTR_TYPE);
        var value = longEdit.ui[type].val(control, btn);
        var warm = srcEle.attr(longEdit.ATTR_MSG);
        if (warm == undefined)
            warm = '该值不合适';
        if (!(srcEle.attr('nullable') != undefined && $(_this).val() == '') && !checkValue(regx, value, type)) {
            alert(warm);
            return false;
        }
        if (srcEle.attr('md5') != undefined)
            value = md5(value);
        $.getJSON(srcEle.attr(longEdit.ATTR_URL), [
                {name: LC.const.UPDATE_ID, value: LC.S.id},
                {name: LC.const.UPDATE_NAME, value: name},
                {name: LC.const.UPDATE_VALUE, value: value}
            ],
            function (rs) {
                if (rs.code == code.FINISH || rs.code == code.SUCCESS) {
                    switch (type) {
                        case 'text':
                            srcEle.text(value);
                            break;
                        case 'password':
                            break;
                        case 'select':
                            srcEle.text($(_this).children('[value=' + value + ']').text());
                            break;
                        case 'textarea':
                            srcEle.text(value);
                            break;
                        default:
                            longController.log(type);
                    }
                    longEdit.ui[type].success(srcEle, value, rs);
                    longEdit.ui[type].cancel(control, btn);
                    srcEle.show();
                } else {
                    alert(rs.msg);
                }
            });

    };
    longEdit.baseUI = function (element) {
        var type = $(element).attr(longEdit.ATTR_TYPE);
        var btnOk = $('<button class="btn btn-default ledit-btn ledit-ok" type="button"><span class="glyphicon glyphicon-ok"></span></button>');
        $(btnOk).attr(longEdit.ATTR_TYPE, type);
        btnOk.click(function () {
            longEdit.save(undefined, this);
        });
        var btnCancel = $('<button class="btn btn-default ledit-btn ledit-cancel" type="button"><span class="glyphicon glyphicon-remove"></span></button>');
        $(btnCancel).attr(longEdit.ATTR_TYPE, type);
        btnCancel.click(function () {
            longEdit.cancel(undefined, this);
        });
        var div = $('<div class="input-group ledit-form"><div class="input-group-btn ledit-group-btn"></div></div>');
        div.children().append(btnOk);
        div.children().append(btnCancel);
        return div;
    };
    longEdit.baseUIRootElement = function (btn) {
        return $(btn).parent().parent();
    };
    longEdit.baseUIVal = function (control, btn) {
        if (control != undefined) {
            return $(control).val();
        }
        var root = longEdit.baseUIRootElement(btn);
        return root.children(':eq(0)').val();
    };
    longEdit.baseUICanncel = function (control, btn) {
        var root;
        if (control != undefined) {
            root = $(control).parent();
        } else {
            root = longEdit.baseUIRootElement(btn);
        }
        root.prev().show();
        root.remove();
    };
    longEdit.addKeyListener = function (control) {
        $(control).keyup(function (e) {
            if (e.which == longEdit.KEY_ESCAPE) {
                longEdit.cancel(this);
                return;
            } else if (e.which == longEdit.KEY_ENTER) {
                longEdit.save(this);
            }
        });
    };
    longEdit.ui = {};
    longEdit.ui.password = {
        init: function (element) {
            var input = $('<input type="password" class="form-control ledit-control" />');
            $(input).attr(longEdit.ATTR_TYPE, $(element).attr(longEdit.ATTR_TYPE));
            longEdit.addKeyListener(input);
            var div = longEdit.baseUI(element);
            div.prepend(input);
            $(element).hide();
            $(element).after(div);
            input.focus();
        },
        val: function (control, btn) {
            return longEdit.baseUIVal(control, btn);
        },
        cancel: function (control, btn) {
            longEdit.baseUICanncel(control, btn);
        },
        success: function (srcEle, value, rs) {
        }
    };
    longEdit.ui.text = {
        init: function (element) {
            var input = longEdit.copyAttr(element, $('<input type="text" class="form-control ledit-control" />'));
            input.val($(element).text());
            input.attr(longEdit.ATTR_TYPE, $(element).attr(longEdit.ATTR_TYPE));
            longEdit.addKeyListener(input);
            var div = longEdit.baseUI(element);
            div.prepend(input);
            $(element).hide();
            $(element).after(div);
            input.focus();
        },
        val: function (control, btn) {
            return longEdit.baseUIVal(control, btn);
        },
        cancel: function (control, btn) {
            longEdit.baseUICanncel(control, btn);
        },
        success: function (srcEle, value, rs) {
            srcEle.text(value);
        }
    };
    longEdit.ui.number = {
        init: function (element) {
            var input = longEdit.copyAttr(element, $('<input type="number" class="form-control ledit-control" />'));
            input.val($(element).text());
            $(input).attr(longEdit.ATTR_TYPE, $(element).attr(longEdit.ATTR_TYPE));
            longEdit.addKeyListener(input);
            var div = longEdit.baseUI(element);
            div.prepend(input);
            $(element).hide();
            $(element).after(div);
            input.focus();
        },
        val: function (control, btn) {
            return longEdit.baseUIVal(control, btn);
        },
        cancel: function (control, btn) {
            longEdit.baseUICanncel(control, btn);
        },
        success: function (srcEle, value, rs) {
            srcEle.text(value);
        }
    };
    longEdit.ui.select = function (element) {
        var input = copyAttr(element, $('<select class="form-control ledit-control"><option value="">资源加载中。。。</option></select>'));
        $.ajax({
            url: $(_this).attr('src'), beforeSend: function () {
            }, success: function (rs, s, q) {
                $(input).html(rs);
                var ops = $(input).children();
                for (var i = 0; i < ops.length; i++) {
                    if ($(ops).eq(i).text() == $(_this).text())
                        $(ops).eq(i).attr('selected', true);
                }
            }
        });
        bindInput(element, input, this.save);
    };
    longEdit.ui.textarea = {
        init: function (element) {
            var text = $(element).text();
            var input = longEdit.copyAttr(element, $('<textarea class="form-control ledit-control clearfix">' + text + '</textarea>'));
            $(input).attr(longEdit.ATTR_TYPE, $(element).attr(longEdit.ATTR_TYPE));
            // longEdit.addKeyListener(input);
            var div = longEdit.baseUI(element);
            div.addClass('ledit-form-block');
            div.prepend(input);
            $(element).hide();
            $(element).after(div);
            input.focus();
        },
        val: function (control, btn) {
            return longEdit.baseUIVal(control, btn);
        },
        cancel: function (control, btn) {
            longEdit.baseUICanncel(control, btn);
        },
        success: function (srcEle, value, rs) {
            srcEle.text(value);
        }
    };
    longEdit.ui.dselect = {
        init: function (element) {
            var text = $(element).text();
            var input = longEdit.copyAttr(element, $('<select class="form-control ledit-control"></select>'));
            var did = $(element).attr(longEdit.ATTR_DID);
            var dict = window.LD.getDicts(did);
            for (var key in dict) {
                input.append('<option value="' + key + '">' + dict[key] + '</option>');
            }
            $(input).attr(longEdit.ATTR_TYPE, $(element).attr(longEdit.ATTR_TYPE));
            // longEdit.addKeyListener(input);
            var div = longEdit.baseUI(element);
            div.addClass('ledit-form-block');
            div.prepend(input);
            $(element).hide();
            $(element).after(div);
            input.focus();
        },
        val: function (control, btn) {
            return longEdit.baseUIVal(control, btn);
        },
        cancel: function (control, btn) {
            longEdit.baseUICanncel(control, btn);
        },
        success: function (srcEle, value, rs) {
            srcEle.text(value);
        }
    };
    longEdit.copyAttr = function (element, input) {
        var attrs = element.attributes;
        for (var i = 0; i < attrs.length; i++) {
            var attr = attrs[i];
            if (LC.regexValidate(/^l_.+/, attr.name)) {
                $(input).attr(attr.name.substr(2), attr.value);
            }
        }
        return input;
    };
    longEdit.bindInput = function (element, input, save) {
        $(input).blur(function (e) {
        });
    };
    window.longEdit = window.LE = longEdit;
})(window);